package com.cie2.janji.pages.admin;

import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;
import org.apache.tapestry5.services.ApplicationGlobals;
import org.apache.tapestry5.upload.services.UploadedFile;

import com.cie2.janji.entities.BaseEntity;
import com.cie2.janji.entities.EntityRole;
import com.cie2.janji.entities.EntityRoleInTime;
import com.cie2.janji.models.EasyIdSelectModel;
import com.cie2.janji.services.BaseEntityService;
import com.cie2.janji.services.EntityRoleInTimeService;
import com.cie2.janji.services.EntityRoleService;

public class CreateEntityRoleInTime {
	static Logger logger = Logger.getLogger(CreateEntityRoleInTime.class
			.getName());

	private Long id;
	
	@Property
	private EntityRoleInTime roleInTime;

	@Property
	private Long entityId;

	@Property
	private Long roleId;
	
	@SuppressWarnings("unused")
	@Property
	@Persist
	private EasyIdSelectModel<BaseEntity> entityModel;	

	@SuppressWarnings("unused")
	@Property
	@Persist
	private EasyIdSelectModel<EntityRole> roleModel;	

	@Inject 
	private ApplicationGlobals applicationGlobals;
	
	// for uploaded file
	@Property
	private UploadedFile file;
	
	@Inject
	private PropertyAccess propertyAccess;
	
	//services
	@Inject
	private EntityRoleInTimeService entityRoleInTimeService;

	@Inject
	private EntityRoleService entityRoleService;
	
	@Inject
	private BaseEntityService baseEntityService;

	@CommitAfter
	Object onSuccess() {
		BaseEntity entity = baseEntityService.load(entityId);
		EntityRole role = entityRoleService.load(roleId);
		
		
		if(file != null) {
			
			String realPath = applicationGlobals.getServletContext().getRealPath("/");
			
			String filename = "R_" + file.getFileName();
			File copied = new File(realPath + File.separatorChar +"images" + File.separatorChar  
					+ File.separatorChar + filename);
	
			file.write(copied);

			roleInTime.setImage(filename);
			
		} else  {
			roleInTime.setImage(entity.getImage());
		}  	
		
		roleInTime.setEntity(entity);
		roleInTime.setRole(role);
		entityRoleInTimeService.add(roleInTime);
		return EntityRoleInTimeList.class;

	}

    void onActivate(Long id) {
    	this.id = id;
    	roleInTime = entityRoleInTimeService.load(id);	
    	entityId = roleInTime.getEntity().getId();
    	roleId = roleInTime.getRole().getId();
    	
	
     }

     Long onPassivate() {
        return id;
     }  

     void beginRender() {
	    	if(null == entityModel) {
	    		logger.debug("Creating model for entity");

	    		List<BaseEntity> entities = baseEntityService.getAll();
	    		logger.debug("Refresh entity size : " + entities.size());
	    		entityModel = new EasyIdSelectModel<BaseEntity>(entities, BaseEntity.class, "name", "id", propertyAccess);    	
	    	} else {
				List<BaseEntity> entities = baseEntityService.getAll();
				logger.debug("Refresh entity size : " + entities.size());
				entityModel.setList(entities);
			}
	    			    	
	    	if(null == roleModel) {

	    		List<EntityRole> roles = entityRoleService.getAll();
	    		roleModel = new EasyIdSelectModel<EntityRole>(roles, EntityRole.class, "name", "id", propertyAccess);    	
	    	} else {
	    		List<EntityRole> roles = entityRoleService.getAll();
				roleModel.setList(roles);
			}

	    }
}
